package com.administrator.customviewtest.view.scrollview;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.Scroller;


/**
 * Created by lsp on 2017/6/6.
 */

public class RefreshLayout extends LinearLayout {
    private boolean interrupt = false;
    private int yMove;
    private int yLast;
    private int y;
    private int i;
    private boolean isRefresh = false;
    private Scroller scroller = new Scroller(getContext());
    private ScrollView scrollView;
    private RecyclerView recyclerView;
    private int rvScrolled = 0;
    private OnRefreshListener onRefreshListener;
    private VelocityTracker velocityTracker;

    public RefreshLayout(Context context) {
        super(context);
        init();
    }


    public RefreshLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RefreshLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {
        addHeadRefresh();
    }

    public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
        this.onRefreshListener = onRefreshListener;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        scrollTo(0, getChildAt(0).getHeight());
        checkScrollView();
        velocityTracker = VelocityTracker.obtain();
    }

    private void checkScrollView() {
        //检测是否有scrollview
        for (int j = 0; j < getChildCount(); j++) {
            Object v = getChildAt(j);
            if (v instanceof ScrollView) {
                scrollView = (ScrollView) v;
            } else if (v instanceof RecyclerView) {
                recyclerView = (RecyclerView) v;

                recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);
                        rvScrolled += dy;
                    }
                });
                recyclerView.getAdapter().registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
                    @Override
                    public void onItemRangeInserted(int positionStart, int itemCount) {
                        super.onItemRangeInserted(positionStart, itemCount);

                    }

                    @Override
                    public void onItemRangeRemoved(int positionStart, int itemCount) {
                        super.onItemRangeRemoved(positionStart, itemCount);
                    }
                });
            }
        }
    }

    private void smoothToScroll(int destaY) {
        scroller.startScroll(0, getScrollY(), 0, destaY, 500);
        invalidate();
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (scroller.computeScrollOffset()) {
            scrollTo(0, scroller.getCurrY());
            postInvalidate();
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        y = (int) ev.getRawY();
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                interrupt = false;
                break;
            case MotionEvent.ACTION_MOVE:
                if (y - yLast > 0) {
                    //检测下拉操作
                    if (isMoveScrollView(ev) || isMoveRecyclerView(ev)) {
                        //如果是在滑动scrollview 不拦截
                        interrupt = false;
                    } else {
                        //否则拦截
                        interrupt = true;
                    }
                } else {
                    //上拉不拦截
                    interrupt = false;
                }
                break;
            case MotionEvent.ACTION_UP:
                interrupt = false;
                break;
        }
        yLast = y;
        return interrupt;
    }

    private boolean isMoveRecyclerView(MotionEvent ev) {
        if (recyclerView == null) {
            return false;
        }
        View head = getChildAt(0);
        //检测点击区域是不是在scrollview
        if (ev.getY() > (recyclerView.getTop() - head.getHeight()) && ev.getY() <=
                (recyclerView.getBottom() - head.getHeight()) && rvScrolled != 0) {
            return true;
        }

        return false;
    }

    private boolean isMoveScrollView(MotionEvent ev) {
        if (scrollView == null) {
            return false;
        }
        View head = getChildAt(0);
        //检测点击区域是不是在scrollview
        if (ev.getY() > (scrollView.getTop() - head.getHeight()) && ev.getY() <=
                (scrollView.getBottom() - head.getHeight()) && scrollView.getScrollY() != 0) {
            return true;
        }

        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //动画还没结束的时候，直接消耗掉事件,不处理。
        if (!scroller.isFinished() || isRefresh) {
            return true;
        }
        velocityTracker.addMovement(event);
        y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                yMove = y - yLast;
                if (yMove >= 0) {
                    scrollBy(0, -yMove / 3);   //  /3为了让下拉有感觉
                    i += yMove / 3;
                }
                break;
            case MotionEvent.ACTION_UP:
                if (i >= getChildAt(0).getHeight()) {
                    smoothToScroll(i - getChildAt(0).getHeight());
                    i = getChildAt(0).getHeight();
                    isRefresh = true;
                    if (onRefreshListener != null) {
                        onRefreshListener.onRefresh();
                    }
                } else {
                    endRefresh();
                }
                velocityTracker.computeCurrentVelocity(1000);
                float velocity = velocityTracker.getYVelocity();
                Log.e("ssss",velocity+"");
                break;
        }
        yLast = y;
        return true;
    }

    public void endRefresh() {
        isRefresh = false;
        smoothToScroll(i);
        i = 0;
    }

    private void addHeadRefresh() {
        setOrientation(LinearLayout.VERTICAL);
        ProgressBar progressBar = new ProgressBar(getContext());
        addView(progressBar, 0);
    }
    private interface OnRefreshListener{
         void onRefresh();
    }
}
